import 'package:base_moudle/index.dart';
import 'package:custom_form/index.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

import '../../../../feature_personal/personal_home/models/industy_model.dart';
import '../../../../models/data_model/HyWorkModel.dart';
import '../../../../new_paltform/define/hy_header.dart';
import '../../db/work_app_model.dart';
import '../hy_work_home_logic.dart';

class HyMeWorkHomeLogic extends HyHomeGetxController {
  List<ZsIndustryModel> zsIndustryModel = <ZsIndustryModel>[];
  ProjectBaseModel? projectBaseModel;

  final Rx<PageStatus> pageStatus = PageStatus.loading.obs;

  @override
  Future<void> onAllData() async {
    pageStatus.value = PageStatus.loading;
    final List<ItemModel> arr = ItemModel.myWorkList();
    final List<HyWorkAppModel> list = await HyWorkAppModel.allList(code: 2);

    if (list.isEmpty) {
      int rank = 1;
      arr.forEach((ItemModel e) async {
        const bool isEnabled = true;
        await HyWorkAppModel.saveModel(
            model: e, isEnabled: isEnabled, rank: rank, code: 2);
        rank++;
      });
    } else {
      // for (HyWorkAppModel item in list) {
      //   int isEnabled = -1;
      //   if (isEnabled != -1) {
      //     await item.saveKeyModel(dict: {'isEnabled': isEnabled});
      //   }
      // }
      int rank = list.length + 1;
      for (final ItemModel e in arr) {
        if (list
            .where((HyWorkAppModel e1) =>
                e.title == e1.appName && e.type == e1.appType)
            .isNullOrEmpty()) {
          await HyWorkAppModel.saveModel(model: e, rank: rank, code: 2);
          rank++;
        }
      }
    }

    allList.value = (await HyWorkAppModel.allList(isEnabled: true, code: 2))
        .map((HyWorkAppModel e) => e.toModel())
        .toList();
    Future.wait([onTimeList(), onItemList()])
        .whenComplete(() => pageStatus.value = PageStatus.normal);
  }

  @override
  Future<void> onTimeList() async {
    timeList.value = (await HyWorkAppModel.timeList(code: 2))
        .map((HyWorkAppModel e) => e.toModel())
        .toList();
  }

  @override
  Future<void> onItemList() async {
    itemList.value = (await HyWorkAppModel.allList(
            isEnabled: true, isShow: true, isRank: true, code: 2))
        .map((HyWorkAppModel e) => e.toModel())
        .toList();
  }

  ///管理时调用
  @override
  Future<void> onUpdateLike() async {
    final List<HyWorkAppModel> list =
        await HyWorkAppModel.allList(isEnabled: true, isShow: true, code: 2);

    for (HyWorkAppModel item in list) {
      if (itemList.where((ItemModel p0) => p0.title == item.appName).isEmpty) {
        await item.saveKeyModel(dict: {'isShow': 0});
      } else {
        final int rank = itemList.indexWhere(
                (ItemModel element) => element.title == item.appName) +
            1;
        await item.saveKeyModel(dict: {'rank': rank, 'isShow': 1});
      }
    }
    onAllData();
  }

  Future<ResultData?> onGetClassificationIndustry() {
    EasyLoading.show(status: '加载中···');
    return get(
            path:
                '/look/lookDict/findByParentId?id=c6993589271d4b73971741166fc4f5a2')
        .then((ResultData? value) {
      EasyLoading.dismiss();
      zsIndustryModel = ZsIndustryModel.listFormJson(value?.data);
    }).catchError((e) {
      EasyLoading.showError('行业性质加载失败');
    });
  }

  Future<ResultData?> onGetFindOne() {
    return get(
            path: '/look/project/findOne',
            queryParams: {'id': 'ec7378c9b62efe745fc3ca99412f1ccf'})
        .then((ResultData? value) {
      projectBaseModel = ProjectBaseModel.fromJson(value?.data);
    });
  }

  Future<ResultData?> onGetFindNearProject() {
    return post(
        path: '/look/project/findNearProject',
        paramType: ParamType.nomarl,
        params: {"userId": ZsMemeryCache.loginDataModel!.userInfo!.userId});
  }

  void onLoadData() {
    Future.wait([onGetFindOne(), onAllData()]);
  }
}
